package lab.exec;

import java.util.ArrayList;
import java.util.List;

public class ProcessQueueGroup<T> {
	private List<ProcessQueue<T>> queueGroup = new ArrayList<ProcessQueue<T>>();
	private int rollIndex = -1;

	public synchronized void addProcessQueue(ProcessQueue<T> queue) {
		queueGroup.add(queue);
	}

	public synchronized boolean removeProcessQueue(ProcessQueue<T> queue) {
		return queueGroup.remove(queue);
	}

	public synchronized void clearProcessQueue() {
		queueGroup.clear();
	}

	public synchronized int getQueueCount() {
		return queueGroup.size();
	}

	public synchronized int getSize() {
		int sum = 0;
		for (ProcessQueue<T> queue : queueGroup) {
			sum += queue.size();
		}
		return sum;
	}

	public synchronized T pollObject() {
		int length = queueGroup.size();
		if (length == 0) {
			return null;
		}
		for (int count = 0; count < length; count++) {
			rollIndex = (++rollIndex) % length;
			ProcessQueue<T> queue = queueGroup.get(rollIndex);
			T obj = queue.poll();
			if (obj != null) {
				return obj;
			}

		}
		return null;
	}
}
